package com.zirenx.sys.controller;

import java.util.Date;
import java.util.List;

import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;
import com.zirenx.annotation.RouteMapping;
import com.zirenx.common.controller.BaseController;
import com.zirenx.common.controller.IController;
import com.zirenx.sys.model.SysRole;
import com.zirenx.sys.service.SysRoleMenuService;
import com.zirenx.utils.LogUtils;
import com.zirenx.utils.R;

@RouteMapping("/sys/role")
public class SysRoleController extends BaseController implements IController {

    @Inject
    SysRoleMenuService sysRoleMenuService;

    @Override
    public void index() {

    }

    @Override
    public void add() {
        SysRole bean = new SysRole();
        setAttr("bean", bean);
    }

    @Override
    public void save() {
    	R r = R.ok();
        try {
            SysRole bean = getBean(SysRole.class,"bean");
            bean.setGmtCreate(new Date());
            bean.save();

            if (bean.getIsSuper() != null && bean.getIsSuper().equals("0")){
                //批量保存角色菜单数据
                String[] menuIds = get("menuIds").split(",");
                sysRoleMenuService.batchSave(bean.getId().toString(),menuIds);
            }
        } catch (Exception e) {
            LogUtils.error(this.getClass(),e);
            r = R.error();
        }
        renderJson(r);
    }

    @Override
    public void edit() {
    	String id = get("id");
    	SysRole bean = new SysRole();
    	bean = bean.findById(id);
    	setAttr("bean", bean);
    }

    @Override
    public void update() {
        R r = R.ok();
        try {
            SysRole bean = getBean(SysRole.class,"bean");
            bean.setGmtModified(new Date());
            bean.update();

            //批量删除原角色菜单数据
            sysRoleMenuService.batchRemove(bean.getId().toString());

            if (bean.getIsSuper() != null && bean.getIsSuper().equals("0")){
                //批量保存角色菜单数据
                String[] menuIds = get("menuIds").split(",");
                sysRoleMenuService.batchSave(bean.getId().toString(),menuIds);
            }

        } catch (Exception e) {
            LogUtils.error(this.getClass(),e);
            r = R.error();
        }
        renderJson(r);
    }

    @Override
    public void remove() {

    }

    @Override
    public void dataAjax() {
        R r = R.ok();
        try {
            SysRole bean = getBean(SysRole.class,"bean");

            SqlPara sqlPara = new SqlPara();
            String sql = "select * from " + bean.getTableName() + " bean where 1=1 ";

            sql += appendSearch(sqlPara);
            sql += " order by bean.id desc";
            sqlPara.setSql(sql);

            Page<SysRole> p = bean.paginate(getPageNumber(), getPageSize(), sqlPara);
            List<SysRole> list = p.getList();

            r.put("rows", list);
            r.put("total", p.getTotalRow());
        } catch (Exception e) {
            LogUtils.error(this.getClass(),e);
            r = R.error();
        }
        renderJson(r);
    }

    @Override
    public void batchRemove() {

    }
}
